package co.edu.usbcali.unidades.dataaccess.dao;

import co.edu.usbcali.unidades.dataaccess.sessionFactory.HibernateSessionFactory;
import co.edu.usbcali.unidades.modelo.MovMesInmueble;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;

import org.hibernate.criterion.Example;

import java.math.BigDecimal;

import java.util.Date;
import java.util.List;
import java.util.Set;


/**
 * A data access object (DAO) providing persistence and search support for
 * MovMesInmueble entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class MovMesInmuebleDAO implements IMovMesInmuebleDAO {
    private static final Log log = LogFactory.getLog(MovMesInmuebleDAO.class);

    // property constants
    //public static final String  MVICODIGO = "mviCodigo";
    public static final String MVICODIGO = "mviCodigo";

    //public static final Date  MVIFECHA = "mviFecha";
    public static final String MVIFECHA = "mviFecha";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            MovMesInmueble Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(MovMesInmueble instance) {
        log.debug("saving MovMesInmueble instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            MovMesInmueble Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(MovMesInmueble instance) {
        log.debug("deleting MovMesInmueble instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            MovMesInmueble Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(MovMesInmueble instance) {
        log.debug("updating MovMesInmueble instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public MovMesInmueble findById(String id) {
        log.debug("finding MovMesInmueble instance with id: " + id);

        try {
            MovMesInmueble instance = (MovMesInmueble) getSession()
                                                           .get(MovMesInmueble.class,
                    id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding MovMesInmueble failed", re);
            throw re;
        }
    }

    public List<MovMesInmueble> findByExample(MovMesInmueble instance) {
        log.debug("finding MovMesInmueble instance by example");

        try {
            List results = getSession().createCriteria("MovMesInmueble")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  MovMesInmueble entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< MovMesInmueble> found by query
        */
    public List<MovMesInmueble> findByProperty(String propertyName, Object value) {
        log.debug("finding MovMesInmueble instance with property: " +
            propertyName + ", value: " + value);

        try {
            String queryString = "from MovMesInmueble as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<MovMesInmueble> findByMviCodigo(Object mviCodigo) {
        return findByProperty(MVICODIGO, mviCodigo);
    }

    public List<MovMesInmueble> findByMviFecha(Object mviFecha) {
        return findByProperty(MVIFECHA, mviFecha);
    }

    /**
    * Find all MovMesInmueble entities.
    *
    * @return List<MovMesInmueble> all MovMesInmueble instances
    */
    public List<MovMesInmueble> findAll() {
        log.debug("finding all MovMesInmueble instances");

        try {
            String queryString = "from MovMesInmueble";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<MovMesInmueble> findByCriteria(String whereCondition) {
        log.debug("finding MovMesInmueble " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from MovMesInmueble model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<MovMesInmueble> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in MovMesInmueble failed", re);
            throw re;
        }
    }

    public List<MovMesInmueble> findPageMovMesInmueble(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding MovMesInmueble findPageMovMesInmueble");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from MovMesInmueble model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from MovMesInmueble model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberMovMesInmueble() {
        log.debug("finding MovMesInmueble count");

        try {
            String queryString = "select count(*) from MovMesInmueble model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
